package com.dao.impl;

import java.util.List;

import javax.enterprise.inject.New;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;


import org.springframework.stereotype.Repository;

import com.hibernate.HibernateSessionFactory;
import com.dao.UserDao;
import com.po.User;
@Repository("userDao")
public class UserDaoImpl extends HibernateDaoSupport implements UserDao{

	@Autowired  
    public void setSessionFactoryOverride(SessionFactory sessionFactory)  
    {    
        super.setSessionFactory(sessionFactory);  
    }  
	@Override
	public boolean check(User user) {
		if(user!=null){
//			Session session=null;  
			String hql = "from User where name=? and password=? and permission=?";		
//			String hql="from Users t where t.name=:name and t.pwd=:pwd ";
//			Query query=this.getSession().createQuery(hql);
//			query.setString("name", user.getName());
//			query.setString("password", user.getPassword());
//			List<User> list=query.list();
			List<User> list = this.getHibernateTemplate().find(hql ,new String[]{user.getName(),user.getPassword(),user.getPermission()});
			if(!list.isEmpty()){
				return true;
			}else {
				return false;
			}
		}else {
			return false;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<User> findAllUser() {
		
		String hql = "from User user order by user.id desc";
		
		return (List<User>)this.getHibernateTemplate().find(hql);
		
	}

	public User findUserById(Integer id) {
		
		User user = (User)this.getHibernateTemplate().get(User.class, id);
		
		return user;
	}

	public void removeUser(User user) {
		
		this.getHibernateTemplate().delete(user);
		
	}

	public void saveUser(User user) {
		
		this.getHibernateTemplate().save(user);

	}

	public void updateUser(User user) {
		
		this.getHibernateTemplate().update(user);
		
	}

}
